package com.example.demo.demo.service;

import com.example.demo.demo.dao.IStudentDao;
import com.example.demo.demo.entity.Student;
import com.example.demo.demo.utlls.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * TODO
 *
 * @author My
 * @date 2020/12/20 10:56
 */
@Service
public class StudentService {

    @Autowired
    IStudentDao studentDao;

    /**
     * 单个保存
     * @param sno 序号
     */
    public void save(String sno) {
        //创建这个学生Student类实例
        Student student = new Student(sno,"张张" + (new Random()).nextInt(),"男",19,"IS");
        studentDao.saveAndFlush(student);
    }


    /**
     * 批量保存
     * @param no 学生数量
     */
    public void batchSave(int no){
        if(no == 0) no = 1;
        List<Student> list = new ArrayList<>();
        for (int i=0;i<no;i++){
            String sno = RandomUtil.newString(5);
            Student student = new Student(sno,"张张" + (new Random()).nextInt(),"男",19,"IS");
            list.add(student);
        }
        studentDao.saveAll(list);
    }
}
